<?php
/**
 * Created by PhpStorm.
 * User: juplus-06
 * Date: 2017/9/13
 * Time: 11:54
 */

namespace app\admin\controller;
use think\Controller;

class Activity extends Controller
{
    public function activitylist(){
        $data = Db('activity')->order('id desc')->paginate(10);
        $page = $data->render();
        $this->assign('page',$page);
        $this->assign('data',$data);
        return view('activity/index');
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $post = input('post.');
            // 获取表单上传文件
            $file = request()->file('image');
            //是否上传图片
            if(!empty($file)){
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size'=>3145728,'ext'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
                $error = $file->getError();
                    //验证文件后缀后大小
                    if(!empty($error)){
                        dump($error);exit;
                    }
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
                    $info->getExtension();
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $info->getSaveName();
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    $post['activity_img'] = $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    $file->getError();
                }
            }else{
                $this->error('请上传图片');
            }
            $num = Db('activity')->insert(['activity_place'=>$post['activity_place'],'activity_time'=>$post['activity_time'],'activity_title'=>$post['activity_title'],'activity_img'=>$post['activity_img']]);
            if($num>0){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        return view('add');
    }


    /**
     * 编辑
     */
    public function edit($id){
        if(request()->isPost()){
            $post = input('post.');
            $file = request()->file('image');
            if(!empty($file)){
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size'=>3145728,'ext'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
                $error = $file->getError();
                    //验证文件后缀后大小
                    if(!empty($error)){
                        dump($error);exit;
                    }
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
                    $info->getExtension();
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $info->getSaveName();
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    $post['activity_img'] = $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    $file->getError();
                }

            }else{
                $post['activity_img'] = Db('activity')->where('id',$post['id'])->value('activity_img');
            }
            $num = Db('activity')->where('id',$post['id'])->update(['activity_place'=>$post['activity_place'],'activity_title'=>$post['activity_title'],'activity_time'=>$post['activity_time'],'activity_img'=>$post['activity_img']]);
            if($num>0){
                $this->success('更新成功');
            }else{
                $this->error('更新失败');
            }
        }
        $data = Db('activity')->where('id',$id)->find();
        $this->assign('data',$data);
        return view('edit');
    }

    /**
     * 用户删除
     */
    public function delete(){
        if(request()->isPost()){
            $post = input('post.');
            $id = $post['uid'];
            if(!empty($id)){
                $num = Db('activity')->where('id',$id)->delete();
                if($num>0){
                    //删除成功
                    return 1;
                }else{
                    //删除失败
                    return 2;
                }
            }else{
                //id不存在
                return 3;
            }
        }else{
            //请求格式不对
            return 4;
        }
    }


    /**
     * 导出
     */
    public function export(){
        $xlsData = Db('activity')->select();
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F");
        $arrHeader = array('活动标题','活动内容','活动地点','活动链接','状态');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['activity_title']);
            $objActSheet->setCellValue('B'.$k, $v['activity_content']);
            // // 图片生成
            // $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            // $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
            // // 设置宽度高度
            // $objDrawing[$k]->setHeight(40);//照片高度
            // $objDrawing[$k]->setWidth(40); //照片宽度
            // /*设置图片要插入的单元格*/
            // $objDrawing[$k]->setCoordinates('C'.$k);
            // // 图片偏移距离
            // $objDrawing[$k]->setOffsetX(30);
            // $objDrawing[$k]->setOffsetY(12);
            // $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            // 表格内容
            $objActSheet->setCellValue('C'.$k, $v['activity_place']);
            $objActSheet->setCellValue('D'.$k, $v['activity_link']);
            $objActSheet->setCellValue('E'.$k, $v['statuid'] == 1?'正常':'失效');


            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(20,20,15,10,10,30,10,15);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[5]);
        $objActSheet->getColumnDimension('B')->setWidth($width[5]);
        $objActSheet->getColumnDimension('C')->setWidth($width[5]);
        $objActSheet->getColumnDimension('D')->setWidth($width[5]);
        $objActSheet->getColumnDimension('E')->setWidth($width[5]);


        $outfile = "活动表.xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }


    /**
     * 修改状态
     */
    public function changestatus(){
        if(request()->isPost()){
            $post = input('post.');
            $id = $post['id'];
            $statuid = $post['statuid'];
            if($statuid == 1){
                $statuid = 0;
                $num = Db('activity')->where('id',$id)->update(['statuid'=>$statuid]);
                if($num>0){
                    return json(['statuid'=>$statuid,'msg'=>'修改成功']);
                }else{
                    $statuid = 1;
                    return json(['statuid'=>$statuid,'msg'=>'修改失败']);
                }
            }else{
                $statuid = 1;
                $num = Db('activity')->where('id',$id)->update(['statuid'=>$statuid]);
                if($num>0){
                    return json(['statuid'=>$statuid,'msg'=>'修改成功']);
                }else{
                    $statuid = 0;
                    return json(['statuid'=>$statuid,'msg'=>'修改失败']);
                }
            }
        }
    }



}